package code.mySpecializedTraining;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @author 26029
 * @date 2025/4/22
 * @description
 */
public class Stack {
    // 150. 逆波兰表达式求值
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<>();
        for (String token : tokens) {
            if ('0' <= token.charAt(0) && token.charAt(0) <= '9' || token.length() >= 2) {
                stack.push(Integer.parseInt(token));
                continue;
            }
            int a1 = stack.pop();
            int a2 = stack.pop();
            switch (token) {
                case "+" -> stack.push(a1 + a2);
                case "-" -> stack.push(a2 - a1);
                case "*" -> stack.push(a1 * a2);
                case "/" -> stack.push(a2 / a1);
            }
        }
        return stack.pop();
    }

    // 224. 基本计算器
    public int calculate(String s) {
        // 每次遇到计算符（计算一次）和右括号（一直计算到左括号）触发计算
        // 本题没有考虑计算符号的优先级（只有+-）
        s = s.replace(" ", "");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '-' && (i == 0 || s.charAt(i - 1) == '('))
                sb.append("0");
            sb.append(c);
        }
        char[] chars = sb.toString().toCharArray();
        int len = chars.length;
        Deque<Integer> num = new ArrayDeque<>();
        Deque<Character> op = new ArrayDeque<>();
        for (int i = 0; i < len; i++) {
            // 数字
            if (Character.isDigit(chars[i])) {
                // 找到完整数字
                int pos = i + 1;
                int sum = chars[i] - '0';
                while (pos < len && Character.isDigit(chars[pos])) {
                    sum = 10 * sum + (chars[pos] - '0');
                    pos++;
                }
                i = pos - 1;
                num.push(sum);
                continue;
            }
            // 左括号
            if (chars[i] == '(') {
                op.push('(');
                continue;
            }
            // 右括号
            if (chars[i] == ')') {
                // 计算
                while (!op.isEmpty() && op.peek() != '(') {
                    calculate(num, op);
                }
                op.pop();
                continue;
            }
            // 操作符
            if (!op.isEmpty() && op.peek() != '(')
                calculate(num, op);
            op.push(chars[i]);
        }
        if (!op.isEmpty())
            calculate(num, op);

        return num.peek();
    }

    private void calculate(Deque<Integer> num, Deque<Character> op) {
        int a1 = num.pop();
        int a2 = num.pop();
        char c = op.pop();
        switch (c) {
            case '+' -> num.push(a2 + a1);
            case '-' -> num.push(a2 - a1);
        }
    }

    public static void main(String[] args) {
        Stack s = new Stack();
        int calculate = s.calculate("1-(     -2)");
        System.out.println(calculate);
    }
}
